package servlet;
import java.io.File;

import javax.servlet.RequestDispatcher;

import javabean.Factory;


public class BookDeleteServlet extends BaseServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service() throws Exception{
		//图片绝对目录
		String path1 = request.getSession().getServletContext().getRealPath("/img/book");
		String bid1 = request.getParameter("bid") ;
		String picurl = request.getParameter("picurl");
		String uid = (String)request.getSession().getAttribute("uid");
		if(uid!=null && !"".equals(uid)){
			long bid = Long.parseLong(bid1);
			try{
				if(Factory.getBookDAOInstance().deleteByBid(bid)){
					File fdel = new File(path1,picurl);
					fdel.delete();
					response.getWriter().write("{\"success\":true,\"msg\":\"删除成功\"}");
				}else{
					response.getWriter().write("{\"success\":false,\"msg\":\"删除失败\"}");
				}
			}
			catch(Exception e){
				response.getWriter().write("{\"success\":false,\"msg\":\"哎呀，服务器出错啦\"}");
			}
		}else{
			//跳转
			RequestDispatcher dispatcher = request.getRequestDispatcher("/login");
			dispatcher.forward(request, response) ;
		}	
	}
}
